stim_type_ms=round((aux_data(1,:)-1)/4*24);

stim_type_fr=zeros(length(frame_times),1);

for ind=1:length(frame_times)
    stim_type_fr(ind)=stim_type_ms(frame_times(ind));
end

location_codes=[1:24];

for knd=1:length(ROIs)
    for ind=1:length(location_codes)
        df=[0;diff(ROIs(knd).activity)];
        df(df<0)=0;
        ret_resp(knd,ind)=mean(ROIs(knd).activity(stim_type_fr==location_codes(ind)));
        ret_dr(knd,ind)=mean(df(stim_type_fr==location_codes(ind)));
    end
end

resp_cells_ind=(max(ret_resp,[],2)-min(ret_resp,[],2))./mean(ret_resp,2)>0.1;

[~,pref_location]=max(ret_resp,[],2);

nc_per_location=hist(pref_location(resp_cells_ind),[1:24]);
figure(4);clf
imagesc(reshape(nc_per_location,[8 3])');
colormap gray
colorbar

figure(2);clf
axes('position',[0 0 1 0.5])
imagesc(reshape(ret_resp(sel_roi,:),[8 3])');
colormap gray
colorbar
axes('position',[0 0.5 1 0.5])
imagesc(reshape(ret_dr(sel_roi,:),[8 3])');
colorbar

figure(3);clf
ret_map=zeros(size(template));
for ind=1:length(ROIs)
    if resp_cells_ind(ind)
        ret_map(ROIs(ind).indices)=(rem(3*pref_location(ind),24)+1);
        %ret_map(ROIs(ind).indices)=rem(ceil(pref_location(ind)/4),2)+1;
    end
end
%subplot(313)
imagesc(ret_map);
colorbar


if exist('data','var')
    if length(stim_type_fr)==size(data,3)
        for ind=location_codes
            full_frame_resp(ind)=mean(mean(mean(data(:,:,stim_type_fr==location_codes(ind)))));
        end
        figure(5);clf
        imagesc(reshape(full_frame_resp,[8 3])');
        colormap gray
        colorbar
    end
end



        
        
        
        
        
        
        



